perm filename A95.TEX[106,PHY] blob sn#826053 filedate 1986-10-07 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00002 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	\magnification\magstephalf
C00022 ENDMK
C⊗;
\magnification\magstephalf
\input macro.tex
\def\today{\ifcase\month\or
  January\or February\or March\or April\or May\or June\or
  July\or August\or September\or October\or November\or December\fi
  \space\number\day, \number\year}
\baselineskip 14pt
\rm
\line{\sevenrm a95.tex[106,phy] \today\hfill}

\font\rmn=cmr9
\bigskip
\line{\bf Iteration.\hfil}

The examples of Pascal programs I have shown so far could have been done
better by a hand-held calculator than by a computer, since I~had to write
a separate formula for each number I~wanted to print. If I~want to perform
a computation that embodies a general pattern, like printing the square
roots of the numbers from~1 to~100, I~find myself mindlessly writing out
line after line of similar statements:

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        WRITELN(SQRT(1));
        WRITELN(SQRT(2));
        WRITELN(SQRT(3));
          $\vdots$
        WRITELN(SQRT(100))
}

\smallskip\noindent
Computers are not only better at calculation than I am; they are also better
at repeating a general pattern of computation with slight variations.
In this example, the pattern is

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        WRITELN(SQRT(I))
}

\smallskip\noindent
and the variation is that {\tt I} takes on the values from~1 to~100. Pascal
provides for automatic repetition with variations, called {\it iteration\/},
if the general pattern can be expressed using a variable ({\tt I},~in this
case), and the variable goes through a sequence of consecutive integers.
A~Pascal program embodying iteration to print the square root table is

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        PROGRAM ITERATE (OUTPUT);
           VAR I: INTEGER;
        BEGIN
        FOR I:=1 TO 100 DO
           WRITELN(SQRT(I))
        END.
}

\smallskip\noindent
Here the second line is a declaration that the values of the variable~{\tt I}
can be any integers (whole numbers). The fourth line is an iterative clause,
repeating the following statement with~{\tt I} taking on successive values
$1,2,3,\ldots,99,100$.

Iteration makes possible the effective use of the enormous speed of current
computers, by specifying concisely a long sequence of similar operations.
Part of the programmer's task is to find a way of looking at a particular
problem as a sequence of similar operations expressible as iteration.

Simple programs which use iteration with variables {\tt I}, {\tt N}, {\tt CAT},
and {\tt DOG} (say) will look like

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        PROGRAM P(OUTPUT);
           VAR I,N,CAT,DOG: INTEGER;
        BEGIN
         $\vdots$
        END.
}

\smallskip\noindent
To save space, we will omit the above standard parts of programs, and show
only fragments of programs to do the actual computation, when the rest of
the program is obvious.

The general form of an {\it iterative statement\/} is:

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        FOR ${\cal{V}}$:=${\cal{E}↓1}$ TO ${\cal{E}↓2}$ DO ${\cal{C}}$
}

\smallskip\noindent
with the effect that

\smallskip
\disleft 25pt:$\bullet$:
$\cal V$ will be given the value ${\cal E}↓1$, and $\cal C$ will be executed.

\disleft 25pt:$\bullet$:
$\cal V$ will be increased by 1, and $\cal C$ will be executed.

\disleft 25pt:$\bullet$:
$\cal V$ will again be increased by 1, and $\cal C$ executed, etc.

\disleft 25pt:$\bullet$:
As soon as $\cal V$ gets larger than ${\cal E}↓2$, the process stops
(even if $\cal C$ has not even been executed once); the iterative command
is then finished.

{\rmn
{\narrower\smallskip\noindent
{\bf Example.} 
\smallskip}
}

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        FOR A:=1 TO 4 DO
           WRITE(2*A+1,2*A)
}

{\rmn
{\narrower\smallskip\noindent
prints:
\smallskip}
}

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        3 2 5 4 7 6 9 8
}

{\rmn
{\narrower\smallskip\noindent
so does:
\smallskip}
}

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        FOR A:=0 TO 3 DO
           WRITE(2*A+3,2*A+2)
}

$$\def\boxit#1{\vbox{\hrule\hbox{\vrule\kern3pt
      \vbox{\kern3pt#1\kern3pt}\kern3pt\vrule}\hrule}}
\setbox4=\vbox{\hsize 23pc \noindent \strut 
\centerline{Rule of Good Programming Practice:}

\noindent {\it Indent the command which is repeated by an iterative
clause, as in the above examples.\/}
\strut}
\boxit{\box4}$$


$$\def\boxit#1{\vbox{\hrule\hbox{\vrule\kern3pt
      \vbox{\kern3pt#1\kern3pt}\kern3pt\vrule}\hrule}}
\setbox4=\vbox{\hsize 23pc \noindent \strut 
\centerline{Rule of Good Programming Practice:}

\noindent {\it Use a descriptive name, where possible, for each iteration
variable. Often an iteration enumerates the elements of a set; if so, 
a~natural name for the iteration variable is the name for an element of
the set. If the iteration is enumerating lines of printed output, the
iteration variable might be {\tt LINE}. If it is keeping track of time, call
it {\tt TIME} or {\tt CLOCK} or {\tt HOURS}.\/}
\strut}
\boxit{\box4}$$

\smallskip
The limitation that iteration variables take on consecutive integer values
does not prevent computing with arbitrary arithmetic series of values.
To print the series $3/7$, $8/7$, $13/7$, $18/7$, $23/7$, $28/7$,
I~can write

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        FOR I:=1 TO 6 DO
           WRITE( (5*I-2)/7 )
}

\smallskip\noindent
where the {\tt 6} was chosen to make the number of repetitions right; 
{\tt I}~gets multiplied by~{\tt 5} 
because the sequence {\tt 3}, {\tt 8}, {\tt 13}, etc., increases by~{\tt 5};
and~{\tt 2} gets 
subtracted to make the numerator~{\tt 3} when {\tt I=1}. Another way
to do it, which experienced programmers learn to prefer, is to count from zero:

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        FOR I:=0 TO 5 DO
           WRITE( (5*I+3)/7 )
}

\smallskip
As a convenience, Pascal allows a form of iteration in which the variable
decreases by~1 at each step:

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        FOR {\rm variable}:={\rm expression$↓1$} DOWNTO {\rm expression$↓2$} DO {\rm statement};
}

\noindent
Both the iterations

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        FOR I:=8 DOWNTO 5 DO
           WRITE(I)
}

\smallskip\noindent
and

\smallskip
{\obeylines\obeyspaces\let =\ \tt
        FOR I:=0 TO 3 DO
           WRITE(8-I)
}

\smallskip\noindent
print {\tt 8 7 6 5}. Experience indicates that decreasing sequences are used
no more than 10\%\ of the time. Note that {\tt DOWNTO} is written as one word.


{\rm
{\narrower\smallskip\noindent
{\bf Exercise.} Write a program which, will, for each number from~2 to~100,
print on one line the number, its square, its cube, and its square root.
\smallskip}

{\narrower\smallskip\noindent
{\bf Exerise.} Write a program which will prevent a temperature conversion
table from degrees Centigrade to degrees Fahrenheit. The relevant formula
is $F=9C/5+32$, where $C$~is the Centigrade temperature and $F$~is the
Fahrenheit temperature. The table should go from $-40$ degrees to
50~degrees Centigrade. 
\smallskip}

{\narrower\smallskip\noindent
(Warning: the formula as given is not in the Pascal language.)
\smallskip}

{\narrower\smallskip\noindent
{\bf Exercise.} Write a program which will, for each number from~10 to~99,
print the number and its logarithm.
\smallskip}

{\narrower\smallskip\noindent
{\bf Exercise.} Write a program which will, for each number in the sequence
$0,0.01, 0.02,\ldots$, $1.00$, print the number, its sine, and its cosine. 
Pascal uses radian measure for angles. Notice that the iteration variable
must be a whole number, changing by~1 at each repetition; you must scale
it down to the desired fractional number, by multiplication or division.
\smallskip}

{\narrower\smallskip\noindent
{\bf Exercise.} Write and execute a Pascal program, using iteration, to
print a conversion table from inches to centimeters in two columns, ranging
from one to fifty inches. The top and bottom of the table should look
like:
$$\vcenter{\halign{\hfil #\hfil\qquad&\hfil #\hfil\qquad\qquad%
&\hfil #\hfil\qquad&\hfil #\hfil\cr
INCHES&CM&INCHES&CM\cr
\phantom{0}1&\phantom{0}2.54&26&\phantom{0}66.04\cr
\phantom{0}2&\phantom{0}5.08&27&\phantom{0}68.58\cr
\phantom{0}$\vdots$&$\vdots$&$\vdots$&$\vdots$\cr
25&63.50&50&127.00\cr}}$$
\smallskip}

{\narrower\smallskip\noindent
{\bf Exercise.} Find the right expression $\cal E$ so that the command:
\smallskip}

{\obeylines\obeyspaces\let =\ \tt
        FOR I:=1 TO 6 DO WRITE(${\cal{E}}$)
}

{\narrower\smallskip\noindent
will print
\smallskip}

{\obeylines\obeyspaces\let =\ \tt
        7 11 15 19 23 27
}

{\narrower\smallskip\noindent
Find another $\cal E$ which makes it print
\smallskip}

{\obeylines\obeyspaces\let =\ \tt
        20 13 6 -1 -6 -15
}

{\narrower\smallskip\noindent
Suggest a general rule. Do the same thing for this command:
\smallskip}

{\obeylines\obeyspaces\let =\ \tt
        FOR I:=0 TO 5 DO WRITE(${\cal{E}}$)
}

{\narrower\smallskip\noindent
Is this easier?
\smallskip}
}

{\rmn
{\narrower\smallskip\noindent
{\bf Exercise.}
Write a program to draw a larger version of the diagonally braced stairway
below. In your drawing, each step should be nine lines above the one
on its right, rather than four, and the stairway should have six steps,
not three.
\smallskip}
}

\medskip
{\obeylines\obeyspaces\let =\ \tt\baselineskip0pt
	*****
	**  *
	* * *
	*  **
	*********
	**  **  *
	* * * * *
	*  **  **
	*************
	**  **  **  *
	* * * * * * *
	*  **  **  **
	*************
}



\bigskip
\line{\copyright 1985 Robert W. Floyd\hfill}
\line{First draft (not published) October 4, 1985;\hfil}
%revised: Date; subsequently revised.\hfill}

\bye